package com.jackstraw.jack_base.datasource.common.service.impl;

import com.jackstraw.jack_base.datasource.common.entity.DataSourceRouting;
import com.jackstraw.jack_base.datasource.common.entity.SnowflakeIdGenerator;
import com.jackstraw.jack_base.datasource.common.service.SnowflakeIdService;
import com.jackstraw.jack_base.util.StringUtil;
import com.jackstraw.jack_base.util.entity.DefaultConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class AbstractSnowflakeIdService<DR extends DataSourceRouting> implements SnowflakeIdService<DR> {

    /**
     * 雪花算法针对不同业务场景的生成类
     * <p>
     * key:业务场景名称
     * value:具体生成类
     */
    private final Map<String, SnowflakeIdGenerator> snowflakeIdGeneratorMap = new ConcurrentHashMap<>();


    /**
     * 应用名称
     */
    @Value("${spring.application.name:default}")
    private String applicationName;

    /**
     * 工作节点生成路径
     */
    private String workNodeMakerPathPrefix;

    @Autowired
    private DR dataSourceRouting;


    public String getWorkNodeMakerPathPrefix() {
        if (StringUtil.StringUtilsBylang3.isEmpty(this.workNodeMakerPathPrefix)) {
            this.setWorkNodeMakerPathPrefix("/SnowFlak/".concat(this.getApplicationName()).concat("/idmaker"));
        }
        return this.workNodeMakerPathPrefix;
    }

    public String getApplicationName() {
        if (StringUtil.StringUtilsBylang3.isEmpty(this.applicationName)) {
            this.setApplicationName(DefaultConstants.DEFAULT);
        }
        return this.applicationName;
    }

    @Override
    public DR getDataSourceRouting() {
        return this.dataSourceRouting;
    }

    public Map<String, SnowflakeIdGenerator> getSnowflakeIdGeneratorMap() {
        return this.snowflakeIdGeneratorMap;
    }

    public void setWorkNodeMakerPathPrefix(String workNodeMakerPathPrefix) {
        this.workNodeMakerPathPrefix = workNodeMakerPathPrefix;
    }

    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }
}
